package Proxy;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IpAndPortFinderProxy360 implements IpAndPortFinder {

	@Override
	public Collection<IpAndPort> find() {
		Collection<IpAndPort> sss=new HashSet<IpAndPort>();
		try {
			Collection<String> ss = find12("http://www.proxy360.cn");
			for(String s:ss){
				sss.addAll(find1(s));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sss;
	}

	public Collection<String> find12(String url) throws IOException {
		
		Set<String> result=new HashSet<String>();
		result.add("http://www.proxy360.cn/default.aspx");
		result.add("http://www.proxy360.cn/Proxy");
		result.add("http://www.proxy360.cn/QQ-Proxy");
		result.add("http://www.proxy360.cn/MSN-Proxy");
		result.add("http://www.proxy360.cn/Region/Brazil");
		result.add("http://www.proxy360.cn/Region/China");
		result.add("http://www.proxy360.cn/Region/America");
		result.add("http://www.proxy360.cn/Region/Taiwan");
		result.add("http://www.proxy360.cn/Region/Japan");
		result.add("http://www.proxy360.cn/Region/Thailand");
		result.add("http://www.proxy360.cn/Region/Vietnam");
		result.add("http://www.proxy360.cn/Region/bahrein");
		return result;
	}

	private InputStream dsfd(String url) {
		InputStream l_urlStream = null;
		try {
			java.net.URL l_url = new java.net.URL(url);
			java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url
					.openConnection();
			l_connection.connect();
			l_urlStream = l_connection.getInputStream();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return l_urlStream;
	}

	public Collection<IpAndPort> find1(String s) {
		String sTotalString = ggggg(s);
		String regex="<span class=\"tbBottomLine\" style=\"width:140px;\">((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)){3})</span><span class=\"tbBottomLine\" style=\"width:50px;\">(\\d{2,4})</span>";
		Pattern p=Pattern.compile(regex);
		Matcher matcher = p.matcher(sTotalString);
		Collection<IpAndPort> result=new HashSet<IpAndPort>();
		while(matcher.find()){
			IpAndPort ipAndPort=new IpAndPort();
			ipAndPort.setIp(matcher.group(1));
			ipAndPort.setPort(matcher.group(5));
			result.add(ipAndPort);
		}
		
		return result;
	}

	private String ggggg(String s) {
		InputStream l_urlStream = dsfd(s);
		String sTotalString = "";
		String sCurrentLine = null;
		java.io.BufferedReader l_reader = new java.io.BufferedReader(
				new java.io.InputStreamReader(l_urlStream));
		try {
			while ((sCurrentLine = l_reader.readLine()) != null) {
				if(!sCurrentLine.equals("")){
					sTotalString += sCurrentLine.trim();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sTotalString;
	}

	public static void main(String[] args) {
		IpAndPortFinderProxy360 s=new IpAndPortFinderProxy360();
		Collection<IpAndPort> a = s.find();
		System.out.println(a.size());
	}
}
